package dao.Impl;

import dao.ProjectRepository;
import entity.Project;
import utils.JDBCTools;

import java.sql.*;
import java.util.ArrayList;

public class ProjectRepositoryImpl implements ProjectRepository {

    /**
     * 从数据库中获取项目信息表
     */
    @Override
    public  ArrayList<Project> getProjectList(){
        Connection connection = JDBCTools.getConnection();
        String sql = "select * from servicemanagementsystem.project";
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        Project project = null;
        ArrayList<Project> projectList = new ArrayList<>();
        try {
            ps = connection.prepareStatement(sql);
            resultSet = ps.executeQuery();
            while (resultSet.next()) {
                Integer id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String content = resultSet.getString(3);
                Date time = resultSet.getDate(4);
                project = new Project(id, name, content,time);
                projectList.add(project);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCTools.release(connection, ps, resultSet);
        }
        return projectList;
    }
    /**
     * 添加项目信息
     */
    @Override
    public boolean addProject(Project project) {
        Connection connection = null;
        String sql = "insert into servicemanagementsystem.project (pid, pname, pcontent, ptime) " +
                "values (?,?,?,?)";
        PreparedStatement ps = null;
        try {
            connection = JDBCTools.getConnection();
            ps = connection.prepareStatement(sql);
            ps.setInt(1, project.getPid());
            ps.setString(2, project.getPname());
            ps.setString(3, project.getPcontent());
            ps.setDate(4, project.getPtime());

            int result = ps.executeUpdate();
            if (result == 1) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("error when add project");
            return false;
        } finally {
            JDBCTools.release(connection, ps, null);
        }
        return false;
    }

    /**
     * 根据项目编号查找数据库返回项目信息对象
     */
    @Override
    public Project findById(Integer projectId) {
        Connection connection = null;
        String sql = "select * from servicemanagementsystem.project where pid=?";
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        Project project = null;
        try {
            connection = JDBCTools.getConnection();
            ps = connection.prepareStatement(sql);
            ps.setInt(1, projectId);
            resultSet = ps.executeQuery();
            while (resultSet.next()) {
                Integer id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String content = resultSet.getString(3);
                Date time = resultSet.getDate(4);
                project = new Project(id, name, content,time);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, ps, resultSet);
        }
        return project;
    }
    /**
     * 修改项目信息
     */
    @Override
    public boolean updateProject(Project project) {
        Connection connection = null;
        String sql = "update servicemanagementsystem.project set pname=? , pcontent=?, ptime=?" +
                "where pid=?";
        PreparedStatement ps = null;
        try {
            connection = JDBCTools.getConnection();
            ps = connection.prepareStatement(sql);
            ps.setString(1, project.getPname());
            ps.setString(2, project.getPcontent());
            ps.setDate(3, project.getPtime());
            ps.setInt(4, project.getPid());
            int result = ps.executeUpdate();
            if (result == 1) {
                return true;
            }
        } catch (SQLException e) {
            System.out.println("error when update project");
            return false;
        } finally {
            JDBCTools.release(connection, ps, null);
        }
        return false;
    }

    /**
     * 根据项目编号删除项目信息
     */
    @Override
    public boolean deleteProject(Integer projectId) {
        Connection connection = null;
        String sql = "delete from servicemanagementsystem.project where pid = ?";
        PreparedStatement ps = null;
        try {
            connection = JDBCTools.getConnection();
            ps = connection.prepareStatement(sql);
            ps.setInt(1, projectId);
            int result = ps.executeUpdate();
            if (result == 1) {
                return true;
            }
        } catch (SQLException e) {
            return false;
        } finally {
            JDBCTools.release(connection, ps, null);
        }
        return false;
    }

}
